<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

class LoginController extends Controller
{
    //登录页展示
    public function index()
    {
        //如果用户已登录
        if(Auth::check()){
            return redirect(route("admin.index"));
        }
        return view("admin.login.login");
    }

    //登录处理
    public function login(Request $request)
    {
        $this->validate($request, [
            'username' => 'required|max:50',
            'password' => 'required|max:50'
        ]);

        $data = Input::except("_token");
        if (!Auth::attempt($data)){
            return redirect(route('admin.login'))->withErrors(["error"=>"登录失败, 请确保输入正确的账号信息"]); //参照路由地址写
        }
        return redirect(route("admin.index"));
    }

    //退出登录
    public function logout(){
        Auth::logout();
        return redirect(route("admin.login"))->with(["success"=>"退出登录成功"]);
    }
}
